#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = "HuJiang";

import requests;
from bs4 import BeautifulSoup;
import re;
from entity.Double_Color_Ball import *;
from datetime import datetime;
import time;

'''
双色球爬虫2
http://www.sfac.xyz/economy/lottery
'''

headers = {
    "Token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIxIiwicm9sZSI6IkFkbWluIiwidXNlckltYWdlIjoiaHR0cDovL3d3dy5zZmFjLnh5ejo4MDAwL2ltYWdlcy9wcm9maWxlLzE2NzAwNzA0NzMwNTUuanBnIiwiaWQiOjEsImV4cCI6MTY4NDgyMzk5NiwidXNlck5hbWUiOiJhZG1pbiIsImlhdCI6MTY4NDczNzU5Nn0.HZrTT3eU8shw9U83OrO-y5CGBhUsHboKxZVH9zskWaU",
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/113.0"
}

# 获取双色球所有数据
def get_all_data(page_count=31):
    all_data = [];
    for page in range(1, page_count + 1):
        all_data += get_dcb_page_data(page);
        time.sleep(3);
    return all_data;

# 获取单页双色球数据
def get_dcb_page_data(current_page=1):
    print("========%d========" % current_page);
    dcbs = [];
    url = "http://538b537e25.zicp.vip:13595/api/economy/bicolorSpheres";

    search = {}
    search["currentPage"] = current_page;
    search["pageSize"] = 100;
    search["keyword"] = "";
    search["sort"] = "issue_no";
    search["direction"] = "asc";

    r = requests.post(url=url, json=search, headers=headers);
    if r.status_code == 200:
        r.encoding = r.apparent_encoding;
        dcb_list = r.json().get("list", []);
        for  dcb in dcb_list:
            dcb = Double_Color_Ball(
                issue_no=dcb.get("issueNo"),
                aware_date=dcb.get("awardDate"),
                red_ball=dcb.get("redBall"),
                blue_ball=dcb.get("blueBall"),
                sales=dcb.get("totalSales"),
                first_number=dcb.get("firstPrizeNumber"),
                second_number=dcb.get("secondPrizeNumber"),
                create_date=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
            );
            print(dcb.__dict__);
            dcbs.append(dcb);
        return dcbs;

if __name__ == '__main__':
    get_all_data(5);
